/*-----------------------------------------------------------------------------

 Copyright 2017 Hopsan Group

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.


 The full license is available in the file LICENSE.
 For details about the 'Hopsan Group' or information about Authors and
 Contributors see the HOPSANGROUP and AUTHORS files that are located in
 the Hopsan source code root directory.

-----------------------------------------------------------------------------*/

#ifndef SIGNALJKLATCH_HPP_INCLUDED
#define SIGNALJKLATCH_HPP_INCLUDED

#include <iostream>
#include "ComponentEssentials.h"
#include "ComponentUtilities.h"
#include "math.h"

//!
//! @file SignalJKlatch.hpp
//! @author Petter Krus <petter.krus@liu.se>
//! @date Sun 29 Mar 2015 12:41:28
//! @brief J-K latch
//! @ingroup SignalComponents
//!
//==This code has been autogenerated using Compgen==
//from 
/*{, C:, HopsanTrunk, componentLibraries, defaultLibrary, Signal, \
Logic}/SignalJKlatch.nb*/

using namespace hopsan;

class SignalJKlatch : public ComponentSignal
{
private:
     int mNstep;
//==This code has been autogenerated using Compgen==
     //inputVariables
     double setCond;
     double resetCond;
     //outputVariables
     double Qstate;
     double notQstate;
     //InitialExpressions variables
     double oldQstate;
     double oldSetCond;
     double oldResetCond;
     //Expressions variables
     //Delay declarations
//==This code has been autogenerated using Compgen==
     //inputVariables pointers
     double *mpsetCond;
     double *mpresetCond;
     //inputParameters pointers
     //outputVariables pointers
     double *mpQstate;
     double *mpnotQstate;
     EquationSystemSolver *mpSolver;

public:
     static Component *Creator()
     {
        return new SignalJKlatch();
     }

     void configure()
     {
//==This code has been autogenerated using Compgen==

        mNstep=9;

        //Add ports to the component
        //Add inputVariables to the component
            addInputVariable("setCond","On condition","",0.,&mpsetCond);
            addInputVariable("resetCond","off condition","",0.,&mpresetCond);

        //Add inputParammeters to the component
        //Add outputVariables to the component
            addOutputVariable("Qstate","Logical state","",0.,&mpQstate);
            addOutputVariable("notQstate","Logical inverse of \
state","",1.,&mpnotQstate);

//==This code has been autogenerated using Compgen==
        //Add constantParameters
     }

    void initialize()
     {
        //Read port variable pointers from nodes

        //Read variables from nodes

        //Read inputVariables from nodes
        setCond = (*mpsetCond);
        resetCond = (*mpresetCond);

        //Read inputParameters from nodes

        //Read outputVariables from nodes
        Qstate = (*mpQstate);
        notQstate = (*mpnotQstate);

//==This code has been autogenerated using Compgen==
        //InitialExpressions
        oldQstate = Qstate;
        oldSetCond = setCond;
        oldResetCond = resetCond;


        //Initialize delays

     }
    void simulateOneTimestep()
     {
        //Read variables from nodes

        //Read inputVariables from nodes
        setCond = (*mpsetCond);
        resetCond = (*mpresetCond);

        //Read inputParameters from nodes

        //LocalExpressions

          //Expressions
          Qstate = limit(limit(setCond*(1 - limit(resetCond,0,1)),0,1) - \
limit(resetCond*(1 - limit(setCond,0,1)),0,1) + limit((1 - \
limit(oldQstate,0,1))*limit(resetCond*setCond,0,1),0,1) + \
limit(oldQstate*limit((1 - limit(resetCond,0,1))*(1 - \
limit(setCond,0,1)),0,1),0,1),0,1);
          oldQstate = Qstate;
          notQstate = 1 - Qstate;

        //Calculate the delayed parts


        //Write new values to nodes
        //outputVariables
        (*mpQstate)=Qstate;
        (*mpnotQstate)=notQstate;

        //Update the delayed variabels

     }
    void deconfigure()
    {
        delete mpSolver;
    }
};
#endif // SIGNALJKLATCH_HPP_INCLUDED
